package org.mobilesynergies.android.epic.service.application;

import java.util.ArrayList;
import java.util.Iterator;

import org.mobilesynergies.android.epic.service.core.ApplicationActivity;
import org.mobilesynergies.android.epic.service.interfaces.ParameterMapImpl;
import org.mobilesynergies.epic.client.remoteui.ArrayParameter;
import org.mobilesynergies.epic.client.remoteui.Parameter;
import org.mobilesynergies.epic.client.remoteui.ParameterMap;
import org.mobilesynergies.epic.client.remoteui.StringParameter;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Browser;
import android.util.Log;
import android.widget.ArrayAdapter;

/**
 * Sends the browser history information to the requesting jid
 * 
 * @author Peter
 */

public class BrowserHistoryProviderActivity extends ApplicationActivity{

	private static final String CLASS_TAG = BrowserHistoryProviderActivity.class.getSimpleName();


	/** 
	 * Called when the activity is first created. 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}


	public ArrayParameter getHistory(){

		String[] projection = new String[] {
				Browser.BookmarkColumns.DATE, 
				//Browser.BookmarkColumns.FAVICON,
				Browser.BookmarkColumns.TITLE,
				Browser.BookmarkColumns.URL,
				Browser.BookmarkColumns.VISITS				
		};

		Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
				projection, null, null,  Browser.BookmarkColumns.DATE + " ASC"
		);
		mCur.moveToFirst();
				
		int dateIdx = mCur.getColumnIndex(Browser.BookmarkColumns.DATE);
		//int faviconIdx = mCur.getColumnIndex(Browser.BookmarkColumns.FAVICON);
		int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
		int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
		int visitsIdx = mCur.getColumnIndex(Browser.BookmarkColumns.VISITS);
		ArrayList<Parameter> array = new ArrayList<Parameter>();
		for(int i =0; i<mCur.getCount(); i++) {
			
			String date = mCur.getString(dateIdx);
			//byte[] favicon = mCur.getBlob(faviconIdx);
			String title = mCur.getString(titleIdx);
			String url = mCur.getString(urlIdx);
			String visits = mCur.getString(visitsIdx);
			String result = "";

			ParameterMap entry = new ParameterMap();
			if(url.length()>0){
				entry.putString("url", url);
			}
			if(title.length()>0){				
				entry.putString("title", title);
			}
			if(date.length()>0){
				entry.putString("date", date);
			}
			if(visits.length()>0){
				entry.putInt("visits", Integer.parseInt(visits));
			}				
			array.add(entry);
			mCur.moveToNext();
		}
		ArrayParameter arrayparameter = new ArrayParameter(array);
		return arrayparameter;
	}


	@Override
	protected void onConnected() {
		Log.d(CLASS_TAG, "connected to the service");
		if(mEpicService.isConnectedToEpicNetwork()){
			sendMessage();
		}
	}


	@Override
	protected void onConnectedToEpicNetwork() {
		sendMessage();
	}


	@Override
	protected void onDisconnected() {
		// TODO Auto-generated method stub
		
	}

	
	void sendMessage(){
		ArrayParameter entries = getHistory();
		ArrayList<Parameter> parameters = entries.getArray();
		Iterator<Parameter> iterParams = parameters.iterator();
		while(iterParams.hasNext()){
			Parameter parameter = iterParams.next();
			ParameterMapImpl map = new ParameterMapImpl();
			map.putParameter("entry", parameter);
			try {			
				mEpicService.sendMessage(BrowserHistoryProviderActivity.class.getName(), map);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	private ParameterMapImpl getSimpleDebugMessage() {
		ParameterMapImpl pm = new ParameterMapImpl();
		pm.putString("teststring", "testmessage lalala");
		return pm;
	}



}